/*
    RoleCall provides a web service that web applications can use to find 
    the roles associated with a user that has been authenticated via OpenID. 
    Also provides a web-based user interface to manage the user roles 
    associated with partner web applications.
    
    Development of this software was supported in part by the David and Lucile 
    Packard Foundation and by the the Office Of Naval Research (ONR) 
     
    Copyright (c) 2012, 
    Monterey Bay Aquarium Research Institute - MBARI ( www.mbari.org )
    Michael Godin ( mikegodin AT users DOT sourceforge DOT net )

    RoleCall is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    RoleCall is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar (see the file COPYING.GPL).  If not, see 
    <http://www.gnu.org/licenses/>.  
*/

package org.mbari.rolecall.model;

import java.io.Serializable;
import java.util.Date;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Usage attempts to track User acceses to Relays. Only start date, end date and
 * count are maintained, not every contact
 * 
 * @author godin
 * 
 */
@SuppressWarnings("serial")
@DatabaseTable(tableName = Usage.TABLE_NAME)
public class Usage implements Serializable {

    public static final String FIELD_NAME_COUNT = "count";

    // field names to simplify QueryBuilder
    public static final String FIELD_NAME_FIRST_DATE = "first_date";

    public static final String FIELD_NAME_LAST_DATE = "last_date";

    public static final String FIELD_NAME_RELAY_ID = "relay_id";

    public static final String FIELD_NAME_USER_ID = "user_id";

    public static final String TABLE_NAME = "Usage";

    @DatabaseField(columnName = FIELD_NAME_COUNT)
    protected int count;

    @DatabaseField(columnName = FIELD_NAME_FIRST_DATE)
    protected Date firstDate;

    @DatabaseField(generatedId = true)
    protected int id;

    @DatabaseField(columnName = FIELD_NAME_LAST_DATE)
    protected Date lastDate;

    @DatabaseField(columnName = FIELD_NAME_RELAY_ID, foreign = true)
    protected Relay relay;

    @DatabaseField(columnName = FIELD_NAME_USER_ID, foreign = true)
    protected User user;

    // No-arg constructor with at package visibility for persistence
    Usage() {
    }

    public Usage(Date firstDate, User user, Relay relay) {
        super();
        this.firstDate = this.lastDate = firstDate;
        this.count = 1;
        this.user = user;
        this.relay = relay;
    }

    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }

    /**
     * @return the firstDate
     */
    public Date getFirstDate() {
        return firstDate;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the lastDate
     */
    public Date getLastDate() {
        return lastDate;
    }

    /**
     * @return the relay
     */
    public Relay getRelay() {
        return relay;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @param id
     *            the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @param lastDate
     *            the lastDate to set
     */
    public void setLastDate(Date lastDate) {
        this.lastDate = lastDate;
        ++this.count;
    }

}